package org.linlinjava.litemall.admin.web2.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req8;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req9;
import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.core.system.SystemStaticParam;
import org.linlinjava.litemall.core.util.R;
import org.linlinjava.litemall.core.util.Util;
import org.linlinjava.litemall.db.dao.TRobotMapper;
import org.linlinjava.litemall.db.domain.TRobot;
import org.linlinjava.litemall.db.domain.TRobotExample;
import org.linlinjava.litemall.db.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 机器人设置
 */
@Api("系统设置[机器人设置]")
@RestController
@RequestMapping("/system/robot")
public class SystemRobotController {

    private static Logger log = LoggerFactory.getLogger(SystemRobotController.class);

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TRobotMapper robotMapper;

    @Autowired
    private StorageService storageService;

    @ApiOperation(value = "销量设置数据")
    @GetMapping("/goodsNumsSetting")
    public Object goodsNumsSetting() {
        Map<String, Object> map = Util.hMap();
        map.put("minInitSaleNum", redisUtil.getInt(SystemStaticParam.minInitSaleNum));
        map.put("maxInitSaleNum", redisUtil.getInt(SystemStaticParam.maxInitSaleNum));
        return R.ok(map);
    }

    @ApiOperation(value = "保存销量设置数据")
    @PostMapping("/save1")
    public Object save1(@RequestBody Req8 req) {
        redisUtil.set(SystemStaticParam.minInitSaleNum, req.getMinInitSaleNum());
        redisUtil.set(SystemStaticParam.maxInitSaleNum, req.getMaxInitSaleNum());
        return R.ok();
    }


    @ApiOperation(value = "机器人列表数据")
    @GetMapping("/robotList")
    public Object robotList() {
        List<TRobot> tRobots = robotMapper.selectByExample(new TRobotExample());
        return R.ok(tRobots);
    }

    @ApiOperation(value = "新增机器人", notes = "传name和img")
    @PostMapping("/addRobot")
    public Object addRobot(@RequestBody Req9 req) {
        TRobot entity = new TRobot();
        entity.setImg(req.getImg());
        entity.setName(req.getName());
        log.info("新增机器人[{}]", req);
        robotMapper.insert(entity);
        return R.ok();
    }

    @ApiOperation(value = "删除机器人", notes = "传id")
    @PostMapping("/delRobot")
    public Object delRobot(@RequestBody Req9 req) {

        TRobot robot = robotMapper.selectByPrimaryKey(req.getId());
        if (robot != null) {
            log.info("删除机器人[{}]", req.getId());
            storageService.deleteByUri(robot.getImg());
            robotMapper.deleteByPrimaryKey(req.getId());
        }
        return R.ok();
    }

}
